package com.koron.css2.newProjectCenter.impl.sealManagement;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.query.PubUpWaterSupplyAreaQueryBean;
import com.koron.css2.newProjectCenter.mapper.PubUpWaterSupplyAreaMapper;
import com.koron.css2.newProjectCenter.vo.PubUpWaterSupplyAreaListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 供水区片区
 * @author xs
 *
 */
public class PubUpWaterSupplyAreaList implements ServerInterface {

	@Override
	@SuppressWarnings("rawtypes")
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		try {
			//将入参转换为QueryBean
			PubUpWaterSupplyAreaQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), PubUpWaterSupplyAreaQueryBean.class);
			//获取Mapper实例
			PubUpWaterSupplyAreaMapper mapper = factory.getMapper(PubUpWaterSupplyAreaMapper.class);

			// 1 select  2 update 3 delect
			if (queryBean.getToDo() != null && queryBean.getToDo() == 2) {
				return saveOrUpdate(mapper, userInfo, queryBean);
			}
			if (queryBean.getToDo() != null && queryBean.getToDo() == 3) {
				return delete(mapper, queryBean);
			}
			if (queryBean.getToDo() != null && queryBean.getToDo() == 4) {
				return  details(mapper, queryBean);
			}

			//设置分页参数
			PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
			//执行SQL
			List<PubUpWaterSupplyAreaListVO> list = mapper.selecList(queryBean);
			/*//获取来源渠道数据字典
			Map<String, String> PIS = Tools.mapDicByCode(factory, "PIS");
			//获取工程类型数据字典
			Map<String, String> PPT = Tools.mapDicByCode(factory, "PPT");
			 */
			//输出分页结果
			MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
			info.setData(new PageInfo<>(list));
			return info;
		} catch (Exception e) {
			factory.close(false);
			logger.error("查询失败" + e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}


	private MessageBean<?> saveOrUpdate(PubUpWaterSupplyAreaMapper thisMapper, UserInfoBean userInfo, PubUpWaterSupplyAreaQueryBean queryBean) {
		if (StringUtils.isEmpty(queryBean.getId())) {
			queryBean.setCreateAccount(userInfo.getUserInfo().getAcount());
			queryBean.setCreateTime(new DateTime());
			queryBean.setCreateName(userInfo.getUserInfo().getName());
			queryBean.setId(Tools.getObjectId());
		} else {
			queryBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
			queryBean.setUpdateTime(new DateTime());
			queryBean.setUpdateName(userInfo.getUserInfo().getName());
		}
		int num = thisMapper.updateOrSave(queryBean);

		MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
		info.setData(num);
		return info;
	}

	private MessageBean<?> delete(PubUpWaterSupplyAreaMapper thisMapper, PubUpWaterSupplyAreaQueryBean queryBean) {
		if (StringUtils.isEmpty(queryBean.getId())) {
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "主键id不能为空", null);
		}
		int num = thisMapper.delete(queryBean.getId());
		MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
		info.setData(num);
		return info;
	}
	private MessageBean<?> details(PubUpWaterSupplyAreaMapper thisMapper, PubUpWaterSupplyAreaQueryBean queryBean) {
		if (StringUtils.isEmpty(queryBean.getId())) {
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "主键id不能为空", null);
		}
		PubUpWaterSupplyAreaListVO  vos = thisMapper.getdetails(queryBean.getId());
		MessageBean<PubUpWaterSupplyAreaListVO> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", PubUpWaterSupplyAreaListVO.class);
		info.setData(vos);
		return info;
	}

}